﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class XeDAO
    {
        #region Selectallthemaloai
        public static List<XeDTO> SelectSoHieuTheoMaLoai(String maloai)
        {
            List<XeDTO> list = new List<XeDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MALOAI", maloai));
                DataTable dt = DataProvider.ExecuteQuery("usp_SelectSoHieuTheoMaLoai",sqlParams);

                foreach (DataRow dr in dt.Rows)
                {
                   XeDTO lx = new XeDTO();
                    lx.MaLoai = dr["MALOAI"].ToString();
                    lx.SoHieu = dr["SOHIEU"].ToString();
                    list.Add(lx);
                }
            }
            catch (Exception ex)
            {
               // throw ex;
            }
            return list;
        }
        #endregion
        public static bool ThemSoHieu(String Sohieu, String maloai)
        {

            #region parameter
            SqlParameter pa0 = new SqlParameter("@hangsx", SqlDbType.VarChar);
            pa0.Value = Sohieu;
            SqlParameter pa1 = new SqlParameter("@maloai", SqlDbType.VarChar);
            pa1.Value = maloai;
            #endregion
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = conn.CreateCommand();
            cmd.Connection = conn;
            #region begin transaction
            SqlTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted, "sohieuxe");
            cmd.Transaction = tran;
            cmd.CommandText = "select * from XE where  MALOAI=@maloai and SOHIEU=@sohieu";
            cmd.Parameters.Add(pa0);
            cmd.Parameters.Add(pa1);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Close();
                cmd.Parameters.Clear();
                tran.Rollback();
                throw new Exception("Số hiệu này đã có");
            }
            dr.Close();
            cmd.Parameters.Clear();
            cmd.CommandText = "insert into XE values(@sohieu,@maloai)";
            cmd.Parameters.Add(pa0);
            cmd.Parameters.Add(pa1);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            tran.Commit();
            #endregion
            conn.Close();
            return true;
        }
     }
}
